*** Setup synthetic controls for defendants 
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop

set seed 1224455689

*** For the single hospitals, take a random sample of 100 control hospitals
local dftlist "StJo Wheaton ElCentro Overlook"
foreach dft of local dftlist {

	use ipop_controls_panel.dta, clear

	** Random sample 100 hospitals
	keep if control == 1 
	keep prvdrnum
	duplicates drop 
	sample 100, count
	save "controls/`dft'_control_id.dta", replace 

	use ipop_controls_panel.dta, clear
	merge m:1 prvdrnum using "controls/`dft'_control_id.dta", keep(match)
	drop _merge control

	save "controls/`dft'_controls.dta", replace
}

*** For the groups of hospitals, sample with replacement to make 100 groups of that size 

**MortonPlant, Shands are both 6 hospitals
local dftlist = "MortonPlant Shands"
foreach dft of local dftlist {
	clear all 
	save control_6group_temp.dta, replace emptyok

	
	foreach i of numlist 1/100{
		use ipop_controls_panel.dta, clear
		
		keep if control == 1 

		keep prvdrnum
		duplicates drop 
		sample 6, count
		save temp_6.dta, replace

		use ipop_controls_panel.dta, clear
		merge m:1 prvdrnum using temp_6.dta, keep(match)
		replace prvdrnum = "`i'"

		collapse (sum) pmt_amt (first) prvdrnum, by(dt)
		append using control_6group_temp.dta

		save control_6group_temp.dta, replace 
	}
	clear all 
	use control_6group_temp.dta, clear
	save "controls/`dft'_controls.dta", replace 
}
rm control_6group_temp.dta 
rm temp_6.dta


*** CHS is 119 hospitals 
local dftlist = "chs"
foreach dft of local dftlist {
	clear all 
	save control_119group_temp.dta, replace emptyok

	
	foreach i of numlist 1/100{
		use ipop_controls_panel.dta, clear
		
		keep if control == 1 

		keep prvdrnum
		duplicates drop 
		sample 119, count
		save temp_119.dta, replace

		use ipop_controls_panel.dta, clear
		merge m:1 prvdrnum using temp_119.dta, keep(match)
		replace prvdrnum = "`i'"

		collapse (sum) pmt_amt (first) prvdrnum, by(dt)
		append using control_119group_temp.dta

		save control_119group_temp.dta, replace 
	}
	clear all 
	use control_119group_temp.dta, clear
	save "controls/`dft'_controls.dta", replace 
}
rm control_119group_temp.dta 
rm temp_119.dta
